package com.emser.unc.server.restlet;

import java.util.ArrayList;

import org.restlet.resource.ServerResource;

import com.emser.unc.serializable.DataStoreVersion;
import com.emser.unc.serializable.ProductDTO;
import com.emser.unc.serializable.SellerDTO;
import com.emser.unc.serializable.SellerProductsDTO;
import com.emser.unc.server.AppLibProduct;
import com.emser.unc.server.AppLibSeller;

public class ProductServerResource extends ServerResource implements ProductResource{
	@Override
	public SellerProductsDTO getProducts(SellerDTO seller) {
		ApplicationRestlet app = (ApplicationRestlet) getApplication();
        if (app.authenticate(getRequest(), getResponse())) {
        	DataStoreVersion version = AppLibSeller.getVersionSeller(seller);
        	SellerProductsDTO result = new SellerProductsDTO();
        	Long versionSellerProduct = seller.getDataSellerversion().getProductsVersion();
        	if((versionSellerProduct == null)){
        		ArrayList<ProductDTO> productsDTO = AppLibProduct.getSellerProducts(seller);
            	result.setProductsDTO(productsDTO);
            	result.setVersion(version.getProductsVersion());
        	}
        	else if(!version.getProductsVersion().equals(seller.getDataSellerversion().getProductsVersion())){
        		ArrayList<ProductDTO> productsDTO = AppLibProduct.getSellerProducts(seller);
            	result.setProductsDTO(productsDTO);
            	result.setVersion(version.getProductsVersion());
        	}
        	else
        	{
        		result.setProductsDTO(new ArrayList<ProductDTO>());
            	result.setVersion(version.getProductsVersion());
        	}
        	return result;
        	
        	
        }
        else
        	return null;
	}

}
